CC=@CC@
CFLAGS=@CFLAGS@
LDFLAGS=@LDFLAGS@
FILE=@FILE@
MAGIC=magic
prefix=@prefix@
exec_prefix=@exec_prefix@
MAGICPATH=@sysconfdir@/binwalk/$(MAGIC)
ETC=$(DESTDIR)@sysconfdir@/binwalk
BIN=$(DESTDIR)@bindir@

all: magi binwalk

binwalk: @DOFILE@ common.o md5.o mparse.o filter.o update.o
	$(CC) $(CFLAGS) -DMAGIC='"$(MAGICPATH).binwalk"' -DMAGIC_CAST='"$(MAGICPATH).bincast"' -DMAGIC_ARCH='"$(MAGICPATH).binarch"' binwalk.c -o binwalk *.o $(LDFLAGS)

file:
	(cd $(FILE)/src && make)
	(cp $(FILE)/src/*.o . && rm -f file.o)
	
common.o:
	$(CC) $(CFLAGS) -c common.c

md5.o:
	$(CC) $(CFLAGS) -c md5.c

mparse.o:
	$(CC) $(CFLAGS) -c mparse.c

filter.o:
	$(CC) $(CFLAGS) -c filter.c

update.o:
	$(CC) $(CFLAGS) -c update.c

magi:
	echo > $(MAGIC).binwalk
	for FILE in magic/*; do \
		cat $$FILE >> $(MAGIC).binwalk ; \
	done

install:
	mkdir -p $(ETC)
	mkdir -p $(BIN)
	cp $(MAGIC).* $(ETC)
	cp binwalk $(BIN)/binwalk

clean:
	rm -f binwalk *.o

cleanall: clean
	rm -rf $(FILE)
	rm -rf config.* *.cache
	rm -f Makefile

distclean: cleanall
	rm -rf $(ETC)
	rm -f $(BIN)/binwalk

